#coding:utf-8

from google.appengine.ext import webapp
from google.appengine.ext.webapp import util
from models import CoolCategory
from models import CoolBookmark
from models import CoolTag
import re
import urllib
import admin
from google.appengine.ext import db
from urllib import URLopener
from random import choice
import zlib

user_agents = [
    'Mozilla/5.0 (Windows; U; Windows NT 5.1; it; rv:1.8.1.11) Gecko/20071127 Firefox/2.0.0.11',
    'Opera/9.25 (Windows NT 5.1; U; en)',
    'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)',
    'Mozilla/5.0 (compatible; Konqueror/3.5; Linux) KHTML/3.5.5 (like Gecko) (Kubuntu)',
    'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.0.12) Gecko/20070731 Ubuntu/dapper-security Firefox/1.5.0.12',
    'Lynx/2.8.5rel.1 libwww-FM/2.14 SSL-MM/1.4.1 GNUTLS/1.2.9'
]

class MyOpener(URLopener, object):
    version = choice(user_agents)

class UpdateCategoryBKCount(webapp.RequestHandler):
    def get(self):
        categorys = CoolCategory.all()
        for category in categorys:
            bookmarkcount = CoolBookmark.all().filter('category = ', category).count()
            category.bookmarkcount = bookmarkcount
            category.put()
            self.response.out.write('%s %d <br>' % (category.name, category.bookmarkcount))

class UpdateTagBKCount(webapp.RequestHandler):
    def get(self):
        tags = CoolTag.all()
        for tag in tags:
            bookmarkcount = CoolBookmark.all().filter('tags = ', tag.name).count()
            tag.bookmarkcount = bookmarkcount
            tag.put()
            self.response.out.write('%s %d <br>' % (tag.name, tag.bookmarkcount)) 
        
        tags = CoolTag.all().filter("bookmarkcount = ", 0)  
        for tag in tags:
            tag.delete()

class UpdateBKPrivilege(webapp.RequestHandler):
    def get(self):
        bks = CoolBookmark.all()
        for bk in bks:
            bk.privilege = 'public'
            bk.put()
            self.response.out.write('%s %s <br>' % (bk.name, bk.privilege))  

class UpdateBKSnapshot(webapp.RequestHandler):
    def get(self):
        bks = CoolBookmark.all()
        for bookmark in bks:
            if not bookmark.zipcontent:
                try:
                    urlopen = MyOpener()
                    fp = urlopen.open(bookmark.url)
                    content = fp.read()
                    fp.close()
                    content = zlib.compress(content, 9)
                    bookmark.zipcontent = db.Blob(content)
                except:
                    bookmark.zipcontent = None
                bookmark.put()                       
         

def main():
  application = webapp.WSGIApplication([('/cronjob/updatecategorybookmarkcount', UpdateCategoryBKCount),
                                        ('/cronjob/updatetagbookmarkcount', UpdateTagBKCount),
                                        ('/cronjob/updatebookmarkprivilege', UpdateBKPrivilege),
                                        ('/cronjob/updatebookmarksnapshot', UpdateBKSnapshot),
                                        ],
                                       debug=True)
  util.run_wsgi_app(application)


if __name__ == '__main__':
  main()
